package com.db.controller;

import com.db.service.PersonService;
import com.db.service.impl.PersonServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

@WebServlet("/removePerson")
public class RemovePersonController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("login");
        if( username == null){
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return;
        }

       Integer personId = Integer.valueOf(request.getParameter("personsId"));
        String rawFace = request.getParameter("rawFace");
        if(!"".equals(rawFace)){
            String rawFilePath = request.getServletContext().getRealPath("upload_repository")+"\\"+rawFace;

            File rawFaceFile = new File(rawFilePath);
            rawFaceFile.delete();
        }
        PersonService ps = new PersonServiceImpl();
        ps.removePerson(personId);
        response.sendRedirect(request.getContextPath()+"/showPersons");

    }
}
